package xyz.app.dependencies;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import xyz.app.Wrapper;

import java.util.function.BiConsumer;
import java.util.function.Function;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public class MemberAccessor {

    @Getter
    private final String name;

    @Getter
    private final Function<@NonNull Wrapper, Wrapper> getter;
    private final BiConsumer<@NonNull Wrapper, Wrapper> setter;

    public Wrapper get(@NonNull Wrapper dto) {
        return getter.apply(dto);
    }

    public void set(@NonNull Wrapper dto, Wrapper member) {
        setter.accept(dto, member);
    }

    public static MemberAccessor of(@NonNull String name, Function<@NonNull Wrapper, Wrapper> getter, BiConsumer<@NonNull Wrapper, Wrapper> setter) {
        return new MemberAccessor(name, getter, setter);
    }
}